package com.example.pricecloudperformancetesting.service;

import com.alibaba.fastjson.JSON;
import com.example.pricecloudperformancetesting.create.CreateSymbolQuote;
import com.example.pricecloudperformancetesting.entity.QuoteHeartbeat;
import com.example.pricecloudperformancetesting.mapper.SymbolConfigDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

/**
 * @author Remy
 * @description :
 * @date 2020-05-25 15:23
 */
@Configuration
@EnableScheduling
public class TCPService implements Runnable {
    private static ServerSocket serverSocket;

    @Autowired
    CreateSymbolQuote createSymbolQuote;

    @Autowired
    SymbolConfigDAO symbolConfigDAO;

    private Integer frequency = 0;

    static {
        try {
            serverSocket = new ServerSocket(9999);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostConstruct
    public void load() {
        frequency = symbolConfigDAO.selectQuoteConfig();
    }


    @Scheduled(cron = "0 0/1 * * * ?")
    private void configureTasks() {
        frequency = symbolConfigDAO.selectQuoteConfig();
    }


    public void startServer() {
        try {
            Socket socket = serverSocket.accept();
            InputStream is = null;
            try {
                //获取客户端传递信息;
                is = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int len = is.read(bytes);
                String temp = new String(bytes, 0, len);
                System.out.println(temp);

                //即将返回客户端信息
                OutputStream out = socket.getOutputStream();
                QuoteHeartbeat Heartbeat = QuoteHeartbeat.builder().dateType(0).dataTime(System.currentTimeMillis() / 1000).version("1.0.0")
                        .data("code:" + 0).key(CreateSymbolQuote.md5("0")).build();
                StringBuffer buffer = new StringBuffer(JSON.toJSONString(Heartbeat));
                buffer.insert(0, "W").insert(buffer.length(), "QUIT");
                out.write(buffer.toString().getBytes());

                while (true) {
                    List<QuoteHeartbeat> quoteHeartbeatList = createSymbolQuote.createQuoteHeartbeat();
                    for (QuoteHeartbeat quoteHeartbeat1 : quoteHeartbeatList) {
                        StringBuffer stringBuffer = new StringBuffer(JSON.toJSONString(quoteHeartbeat1));
                        stringBuffer.insert(0, "W").insert(stringBuffer.length(), "QUIT");
                        out.write(stringBuffer.toString().getBytes());
                        out.flush();
                        Thread.sleep(frequency);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void startServerThread() {
        try {
            Socket socket = serverSocket.accept();
            InputStream is = null;
            try {
                //获取客户端传递信息;
                is = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int len = is.read(bytes);
                String temp = new String(bytes, 0, len);
                System.out.println(temp);

                //即将返回客户端信息
                OutputStream out = socket.getOutputStream();
                QuoteHeartbeat Heartbeat = QuoteHeartbeat.builder().dateType(0).dataTime(System.currentTimeMillis() / 1000).version("1.0.0")
                        .data("code:" + 0).key(CreateSymbolQuote.md5("0")).build();
                StringBuffer buffer = new StringBuffer(JSON.toJSONString(Heartbeat));
                buffer.insert(0, "W").insert(buffer.length(), "QUIT");
                out.write(buffer.toString().getBytes());

                while (true) {
                    List<QuoteHeartbeat> quoteHeartbeatList = createSymbolQuote.createQuoteHeartbeat();
                    for (QuoteHeartbeat quoteHeartbeat1 : quoteHeartbeatList) {
                        StringBuffer stringBuffer = new StringBuffer(JSON.toJSONString(quoteHeartbeat1));
                        stringBuffer.insert(0, "W").insert(stringBuffer.length(), "QUIT");
                        out.write(stringBuffer.toString().getBytes());
                        out.flush();
                        Thread.sleep(frequency);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getId());
        try {
            Socket socket = serverSocket.accept();
            InputStream is = null;
            try {
                //获取客户端传递信息;
                is = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int len = is.read(bytes);
                String temp = new String(bytes, 0, len);
                System.out.println(temp);

                //即将返回客户端信息
                OutputStream out = socket.getOutputStream();
                QuoteHeartbeat Heartbeat = QuoteHeartbeat.builder().dateType(0).dataTime(System.currentTimeMillis() / 1000).version("1.0.0")
                        .data("code:" + 0).key(CreateSymbolQuote.md5("0")).build();
                StringBuffer buffer = new StringBuffer(JSON.toJSONString(Heartbeat));
                buffer.insert(0, "W").insert(buffer.length(), "QUIT");
                out.write(buffer.toString().getBytes());

                while (true) {
                    List<QuoteHeartbeat> quoteHeartbeatList = createSymbolQuote.createQuoteHeartbeat();
                    for (QuoteHeartbeat quoteHeartbeat1 : quoteHeartbeatList) {
                        StringBuffer stringBuffer = new StringBuffer(JSON.toJSONString(quoteHeartbeat1));
                        stringBuffer.insert(0, "W").insert(stringBuffer.length(), "QUIT");
                        out.write(stringBuffer.toString().getBytes());
                        out.flush();
                        Thread.sleep(frequency);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
